package com.example.dangerouscargodriver.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baiju.netmanager.Http.IHttpRequest;
import com.baiju.style.citylist.Toast.ToastUtils;
import com.bumptech.glide.Glide;
import com.example.dangerouscargodriver.R;
import com.example.dangerouscargodriver.controller.AuthController;
import com.example.dangerouscargodriver.entry.CompanyDetailInfo;
import com.example.dangerouscargodriver.entry.CompanyInfo;
import com.example.dangerouscargodriver.module.ResponseInfo;
import com.example.dangerouscargodriver.param.RemoteAPI;
import com.example.dangerouscargodriver.param.RemoteAPICmd;
import com.example.dangerouscargodriver.widget.ActionBar;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;

public class CompanyDetailActivity extends HttpRequestActivity {

    private static final String TAG = "CompanyDetailActivity";
    private ActionBar mActionBar = null;
    private TextView mCompanyName = null;
    private TextView mCompanyStatus = null;
    private TextView mCompanyAddress = null;
    private TextView mCompanyType = null;
    private TextView mResourceType = null;
    private TextView mCarType = null;
    private TextView mContact = null;
    private TextView mTelephone = null;
    private TextView mPhone = null;
    private ImageView mCert6 = null;
    private ImageView mCert7 = null;
    private ImageView mCert8 = null;
    private ImageView mMainPic = null;

    private String mCid = null;
    private CompanyDetailInfo mCompanyDetailInfo =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.initHttpComponent(CompanyDetailActivity.class);
        setContentView(R.layout.activity_company_detail);
        mCid = getIntent().getStringExtra("cid");

        mCompanyName = findViewById(R.id.tv_text_company_name);
        mCompanyAddress = findViewById(R.id.tv_text_company_address);
        mCompanyType = findViewById(R.id.tv_text_company_type);
        mResourceType = findViewById(R.id.tv_text_resource_type);
        mCarType = findViewById(R.id.tv_text_car_type);
        mContact = findViewById(R.id.tv_text_contact);
        mTelephone = findViewById(R.id.tv_text_contact_telephone);
        mPhone = findViewById(R.id.tv_text_contact_phone);
        mCert6 = findViewById(R.id.iv_cert6);
        mCert7 = findViewById(R.id.iv_cert7);
        mCert8 = findViewById(R.id.iv_cert8);
        mCompanyStatus = findViewById(R.id.tv_text_company_status);

        mMainPic = findViewById(R.id.iv_company_main_pic);

        mActionBar = findViewById(R.id.actionBarCompanyDetail);
        mActionBar.setOnBackClickListener( new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        executeGetCompanyInfo();
    }

    private void executeGetCompanyInfo(){
        HashMap<String, String> header = new HashMap<String, String>();
        header.put("token", AuthController.Instance().getToken());

        sendHttpRequest(
                IHttpRequest.RequestMethod.GET_METHOD,
                RemoteAPI.REQUEST_V1_USER_COMPANY + "/" + mCid,
                header,
                null,
                RemoteAPICmd.REQUEST_V1_USER_COMPANY);
    }

    private void refreshView(){
        if(mCompanyDetailInfo != null){
            mCompanyName.setText(mCompanyDetailInfo.getName());
            mCompanyAddress.setText(mCompanyDetailInfo.getArea());
            mCompanyType.setText(mCompanyDetailInfo.getClassname());
            String resType = "";
            for(int index = 0; index < mCompanyDetailInfo.getCore_attr().size(); index++){
                if(mCompanyDetailInfo.getCore_attr().get(index).getIs_selected() == 1) {
                    resType += mCompanyDetailInfo.getCore_attr().get(index).getClassname() + "， ";
                }
            }
            if(resType.length() > 0){
                resType = resType.substring(0, resType.length() - 2);
            }
            mResourceType.setText(resType);
            String carType = "";
            for(int index = 0; index < mCompanyDetailInfo.getTruck_attr().size(); index++){
                if(mCompanyDetailInfo.getTruck_attr().get(index).getIs_selected().equals("1")) {
                    carType += mCompanyDetailInfo.getTruck_attr().get(index).getClassname() + "， ";
                }
            }
            if(carType.length() > 0){
                carType = carType.substring(0, carType.length() - 2);
            }
            mCarType.setText(carType);
            mContact.setText(mCompanyDetailInfo.getContact());
            mTelephone.setText(mCompanyDetailInfo.getMobilephone());
            mPhone.setText(mCompanyDetailInfo.getTel());
            if(mCompanyDetailInfo.getCert6() != null && !mCompanyDetailInfo.getCert6().equals("")) {
                mCert6.setBackgroundResource(0);
                Glide.with(getBaseContext()).load(mCompanyDetailInfo.getCert6()).into(mCert6);
            }
            if(mCompanyDetailInfo.getCert7() != null && !mCompanyDetailInfo.getCert7().equals("")) {
                mCert7.setBackgroundResource(0);
                Glide.with(getBaseContext()).load(mCompanyDetailInfo.getCert7()).into(mCert7);
            }
            if(mCompanyDetailInfo.getCert8() != null && !mCompanyDetailInfo.getCert8().equals("")) {
                mCert8.setBackgroundResource(0);
                Glide.with(getBaseContext()).load(mCompanyDetailInfo.getCert8()).into(mCert8);
            }
            if(mCompanyDetailInfo.getMain_pic() != null && !mCompanyDetailInfo.getMain_pic().equals("")) {
                mCert8.setBackgroundResource(0);
                Glide.with(getBaseContext()).load(mCompanyDetailInfo.getMain_pic()).into(mMainPic);
            }

            mCompanyStatus.setText(mCompanyDetailInfo.getAdultstatus_name());
        }
    }

    @Override
    protected void onHttpResponse(int actionId, String response) {
        try {
            JSONObject res = new JSONObject(response);
            Gson gson = new Gson();
            if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
                Log.d(TAG, "onResponse: " + response);
                ResponseInfo responseInfo = gson.fromJson(response, ResponseInfo.class);
                if(responseInfo != null && responseInfo.getStatus() == 1){
                    mCompanyDetailInfo = gson.fromJson(res.getString("data"), CompanyDetailInfo.class);
                    refreshView();
                }
            }
            else{

            }
        }
        catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onHttpErrorResponse(int actionId, String response) {
        if(actionId == RemoteAPICmd.REQUEST_V1_USER_COMPANY){
            ToastUtils.showLongToast(this, "获取详细公司信息失败");
        }
    }
}